import { Component } from 'react';
import { Boxs, SearchBar, Badge, ScrollList, Button, TabBar, Dialog, Toast} from 'saltui';
const { HBox, VBox, Box } = Boxs;
const { IconButton } = Button;
import { URLS,nodataIcon } from '../../../app/variables';
import MyTabBar from 'components/tab_bar';
import DB from '../../../app/db';
import './PageHome.less';

const ItemCycle  =  React.createClass({
  detailsClick: function(event) {
    let t = this;
    DB.SomeModuleAPI.entityIsExist({id:t.props.id}).then((content) => {
      if(content == 'exist'){
        let url ='meeting/myMeetingDetails/'+t.props.id + '/myMeeting';
        location.hash=url;
      }else{
        Toast.show({
            type: 'error',
            content: '当前数据已删除',
        });
        t.props.refreshList();
      }
    }).catch((error) => {
    });
  },
  render:function() {
       return <div>         
        <HBox className="t-BCf t-P16 t-PR0 dd-bottom-border" onClick={this.detailsClick}>
          <Box flex={1}>
            <div className="t-omit t-FC3 t-FS16 t-LH1_3 t-MR8">
              {this.props.applyRecordName}
            </div>
            <HBox className="t-FCddfontblue-40 t-LH1_3 t-PT8 t-FS14" vAlign="center">
              <i className="iconfont icon-time t-MR4 t-FS14"></i>
              <span className="t-omit">{this.props.startDate}~{this.props.endDate}</span>
            </HBox>
            <HBox className="t-FCddfontblue-40 t-LH1_3 t-PT6 t-FS14" vAlign="center">
              <i className="iconfont icon-place t-MR4 t-FS14"></i>
              <span className="t-omit">{this.props.boardroomName == null || this.props.boardroomName == "" ? "无" : this.props.boardroomName}</span>
            </HBox>
          </Box>
          <VBox vAlign="center" className="z-AIFE t-MR16">
            <Box className={this.props.status==3?'z-WFC t-H22 t-LH22 t-DIB light-badge badge-lightred':'t-DN'}>已取消</Box>
            <Box className={this.props.status==2?'z-WFC t-H22 t-LH22 t-DIB light-badge badge-lightgreen':'t-DN'} >已驳回</Box>
            <Box className={this.props.status==5?'z-WFC t-H22 t-LH22 t-DIB light-badge badge-lightgrey':'t-DN'}>已结束</Box>
            <Box className={this.props.status==0?'z-WFC t-H22 t-LH22 t-DIB light-badge badge-lightgrey':'t-DN'}>审批中</Box>
            <Box className={this.props.status==4?'z-WFC t-H22 t-LH22 t-DIB light-badge badge-lightgreen':'t-DN'}>进行中</Box>
            <Box className={this.props.status==1?'z-WFC t-H22 t-LH22 t-DIB light-badge badge-lightred':'t-DN'}>未召开</Box>
          </VBox>
        </HBox>           
      </div>
    }
 });

export default class PageHome extends Component {


 constructor(props) {
    super(props);

   Dialog.hide();
   //修改待办列表页tab默认值
   document.getElementById("sendTabIndex").value = "0";
    //关闭加载页的遮罩效果
    $(".loading-containter").hide();
    //禁用ios弹性事件
    dd.ui.webViewBounce.disable();
    //是否需要底部tab页
    var hasBottomTab =  document.getElementById("hasBottomTab").value;
    var winHeight = getScrollHigh(1, 0, hasBottomTab);
    
    this.state = {
      id:this.props.params.id,
      search:'',
      noDataTip:'暂无数据',
      winHeight:winHeight,
      hasBottomTab: hasBottomTab
    };

   let t = this;
    let userId = document.getElementById("userHiddenId").value;
    var isHelpNew = 1;
    dd.util.domainStorage.getItem({ //帮助按钮
       name:'meeting_helpNew_'+userId , // 存储信息的key值
       onSuccess : function(info) {
            var helpNew = info.value;
            if( typeof(helpNew) == 'undefined' || helpNew == '' ){
              isHelpNew = 2;
            }
            //初始化按钮
            t.initButton(isHelpNew);
       },
       onFail : function(err) {
            //alert(JSON.stringify(err));
       }
    });
  }

  componentDidMount(){
    var page_title = "会议";
    $("title").text(page_title);
    var thirdMenuType = getLoginUserInfo().thirdMenuType;
    if(thirdMenuType == 'DING'){
      dd.biz.navigation.setTitle({
        title : page_title,//控制标题文本，空字符串表示显示默认文本
        onSuccess : function(result) {},
        onFail : function(err) {}
      });
    }else if(thirdMenuType =='MH'){
      !!emp&&emp.window.setTitle({
        title: {
            text: page_title,
            icon: '',
            actionmenu: {}
        },
      });
    }
    searchBarLimit(20);
  }

  // 请求参数
  beforeFetch(data,from){
    if (from.title == undefined) {
      data.condition = this.state.search;
    } else {
      data.condition = from.title;
    }
    data.id = this.state.id;
    return data;
  }

   //搜索
   onSearch(keyword) {
      this.setState({
        search:keyword,
        noDataTip:'无搜索结果'
      });
      this.refs.meeting_cycMeeting_ref.fetchData({title:keyword});
   }

  //退出
  onExit(){
    this.setState({
      search:'',
      noDataTip:'暂无数据'
    });
    this.refs.meeting_cycMeeting_ref.fetchData({title:''});
  }

  scanQrcode(){
    var thirdMenuType = getLoginUserInfo().thirdMenuType;
    let ipPort = getLoginUserInfo().contextPageUrl;
    //第三方标识：DING（钉钉），WX（企业微信），MH（移动门户）
    if(thirdMenuType=='DING'){
      dd.biz.util.scan({
        type: "qrCode" , // type 为 all、qrCode、barCode，默认是all。
        onSuccess: function(data) {
          pageSkipDirectly(ipPort + data.text);
        },
        onFail : function(err) {}
      })
    }else if(thirdMenuType=='WX'){
      wx.scanQRCode({
        desc: 'scanQRCode desc',
        needResult: 1, // 默认为0，扫描结果由企业微信处理，1则直接返回扫描结果，
        scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是条形码（一维码），默认二者都有
        success: function(res) {
          pageSkipDirectly(ipPort + res.resultStr);
        },
        error: function(res) {
            if (res.errMsg.indexOf('function_not_exist') > 0) {
                alert('版本过低请升级')
            }
        }
      });
    }else if(thirdMenuType=='MH'){
      !!emp&&emp.scanQRCode({
        needResult: 1, // 默认为0，扫描结果由App处理，1则直接返回扫描结果，
        scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
        success: function (res) {
          pageSkipDirectly(ipPort + res.resultStr);
        }
      });
    }
  }

  fetchData(param){
    this.refs.meeting_cycMeeting_ref.fetchData({title:''});
  }

  render() {
    const t = this;
    let listHeight = {height: this.state.winHeight};
    return (
      <div className="page-meeting">
        <div className="my-meeting">
              <SearchBar hasHistory={false} instantSearch={false} onSearch={this.onSearch.bind(this)}  ref='cycleMeeting_search' placeholder='请输入会议名称' onExit={this.onExit.bind(this)}>
              </SearchBar>
              <div style={listHeight}>
                <ScrollList
                  url={URLS.getCycleMeetingByCycleId}
                  dataType="json"
                  pageSize={10}
                  beforeFetch={this.beforeFetch.bind(this)}
                  noDataTip={this.state.noDataTip}
                  processData={(data) => data}
                  ref = 'meeting_cycMeeting_ref'
                  noDataImage={nodataIcon}
                  scrollLoad={true}
                  currentPageKey="page">
                  <ItemCycle refreshList={this.fetchData.bind(this)}/>
                </ScrollList>
              </div>
        </div>
        <div className={this.state.hasBottomTab == "0"?"t-DN":""}>
          <MyTabBar activeIndex={1} />
        </div>
        <IconButton className="add-new-page t-BCddblue" onClick={this.scanQrcode.bind(this)} ><i className="iconfont icon-scan t-FCf"></i></IconButton>
      </div>
    );
  }
}


